package com.ljl.ningwei.controller;

import com.ljl.ningwei.common.result.Result;
import com.ljl.ningwei.common.utils.TimeUtil;
import com.ljl.ningwei.domain.PageVO;
import com.ljl.ningwei.entity.User;
import com.ljl.ningwei.service.CategoryService;
import com.ljl.ningwei.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 吕佳乐
 * 2025-03-13 20:39
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    /**
     * 注册接口
     */
    @RequestMapping("/user/register")
    public Result<Void> register(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "password") String password,
            @RequestParam(name = "phone") String phone,
            @RequestParam(name = "avatar", required = false) String avatar
    ) {
        // 创建加密器
        User user = User.builder()
                .account(account)
                .name(name)
                .password(password)
                .phone(phone)
                .avatar(avatar)
                .createTime(TimeUtil.getTime())
                .updateTime(TimeUtil.getTime())
                .isDeleted(0)
                .build();
        userService.register(user);
        return Result.success();
    }

    /**
     * 登录接口
     */
    @RequestMapping("/user/login")
    public Result<String> login(
            @RequestParam(name = "account") String account,
            @RequestParam(name = "password") String password) {
        return Result.success(userService.login(account,password));
    }


}
